package com.xcy.quickhand.spider;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.xcy.quickhand.model.A;
import com.xcy.quickhand.model.Video;

import java.util.*;

/**
 * @author xcy
 * @version V1.0
 * @description
 * @date 2023/2/11 18:00
 */
public class HttpTest {
    public static void main(String[] args) {

        String url = "https://www.kuaishou.com/graphql";
        // 用户id
        String userId = "3xz7ys9jnhrdhew";
        // cookie
        String cookie = "did=web_1dbcb1fddff24c74944acecb8029726d; didv=1675945966000; kpf=PC_WEB; clientid=3; userId=734799445; kuaishou.server.web_st=ChZrdWFpc2hvdS5zZXJ2ZXIud2ViLnN0EqABcFm3_q4_9Jd_rMPhGZ5T2Dc4NfZU2bMzF03wx0VbAfi1RWiFVyfFuoVz8Y0kq10_knRSwXysGIyfZ8JuxIRBNdlJDiBIFlYu1VPYUAFyHtJJ_96BvLIX6o4Pu6PqOma_hwBmVTOFKaOX8T0SijCnHxH23wMbMM8Ka46oDT2k8ht7_NMOV-gqxKotdc3VzNFiAQTpOvEJEXxTjzwE_wPjHRoSg3ZkWJHNsQvvc8vsvUksYr6BIiCFNuBRFsVwLXF7olm2taXb64CL8jYEXZ9p_phinc_qTygFMAE; kuaishou.server.web_ph=9863e984c79c33b99a58f9a260736d2384d8; kpn=KUAISHOU_VISION";


        Map map = new HashMap<String, String>() {
            {
                put("cookie", "did=web_1dbcb1fddff24c74944acecb8029726d; didv=1675945966000; kpf=PC_WEB; clientid=3; userId=734799445; kuaishou.server.web_st=ChZrdWFpc2hvdS5zZXJ2ZXIud2ViLnN0EqABcFm3_q4_9Jd_rMPhGZ5T2Dc4NfZU2bMzF03wx0VbAfi1RWiFVyfFuoVz8Y0kq10_knRSwXysGIyfZ8JuxIRBNdlJDiBIFlYu1VPYUAFyHtJJ_96BvLIX6o4Pu6PqOma_hwBmVTOFKaOX8T0SijCnHxH23wMbMM8Ka46oDT2k8ht7_NMOV-gqxKotdc3VzNFiAQTpOvEJEXxTjzwE_wPjHRoSg3ZkWJHNsQvvc8vsvUksYr6BIiCFNuBRFsVwLXF7olm2taXb64CL8jYEXZ9p_phinc_qTygFMAE; kuaishou.server.web_ph=9863e984c79c33b99a58f9a260736d2384d8; kpn=KUAISHOU_VISION");
                put("content-type", "application/json");
                put("Host", "www.kuaishou.com");
                put("Origin", "https://www.kuaishou.com");
                put("Referer", "https://www.kuaishou.com/profile/3xgz9zaku7hig96");
                put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36");
            }
        };


        String response = HttpRequest.post(url).header("cookie", cookie)
                .header("content-type", "application/json")
                .header("Host", "www.kuaishou.com")
                .header("Origin", "https://www.kuaishou.com")
                .header("Referer", "https://www.kuaishou.com/profile/3xgz9zaku7hig96")
                .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36")
                .body(
                        "{\n" +
                                "    \"operationName\": \"visionProfilePhotoList\",\n" +
                                "    \"variables\": {\n" +
                                "        \"userId\": \"" + userId + "\",\n" +
                                "        \"pcursor\": \"\",\n" +
                                "        \"page\": \"profile\"\n" +
                                "    },\n" +
                                "    \"query\": \"fragment photoContent on PhotoEntity {\\n  id\\n  duration\\n  caption\\n  originCaption\\n  likeCount\\n  viewCount\\n  realLikeCount\\n  coverUrl\\n  photoUrl\\n  photoH265Url\\n  manifest\\n  manifestH265\\n  videoResource\\n  coverUrls {\\n    url\\n    __typename\\n  }\\n  timestamp\\n  expTag\\n  animatedCoverUrl\\n  distance\\n  videoRatio\\n  liked\\n  stereoType\\n  profileUserTopPhoto\\n  musicBlocked\\n  __typename\\n}\\n\\nfragment feedContent on Feed {\\n  type\\n  author {\\n    id\\n    name\\n    headerUrl\\n    following\\n    headerUrls {\\n      url\\n      __typename\\n    }\\n    __typename\\n  }\\n  photo {\\n    ...photoContent\\n    __typename\\n  }\\n  canAddComment\\n  llsid\\n  status\\n  currentPcursor\\n  tags {\\n    type\\n    name\\n    __typename\\n  }\\n  __typename\\n}\\n\\nquery visionProfilePhotoList($pcursor: String, $userId: String, $page: String, $webPageArea: String) {\\n  visionProfilePhotoList(pcursor: $pcursor, userId: $userId, page: $page, webPageArea: $webPageArea) {\\n    result\\n    llsid\\n    webPageArea\\n    feeds {\\n      ...feedContent\\n      __typename\\n    }\\n    hostName\\n    pcursor\\n    __typename\\n  }\\n}\\n\"\n" +
                                "}"
                ).execute().body();


        A a = JSONUtil.toBean(response, A.class);

        // 下一页游标
        String nextCursor = a.getData().getVisionProfilePhotoList().getPcursor();

        List<A.DataDTO.VisionProfilePhotoListDTO.FeedsDTO> feeds = a.getData().getVisionProfilePhotoList().getFeeds();

        List videoList = new ArrayList<Video>();

        for (int i = 0; i < feeds.size(); i++) {
            A.DataDTO.VisionProfilePhotoListDTO.FeedsDTO feedsDTO = feeds.get(i);
            A.DataDTO.VisionProfilePhotoListDTO.FeedsDTO.AuthorDTO author = feedsDTO.getAuthor();

            // 作者id
            String authorId = author.getId();
            System.out.println("作者id " + authorId);
            // 作者头像
            String headerUrl = author.getHeaderUrl();
            System.out.println("作者头像 " + headerUrl);
            // 作者名称
            String authorName = author.getName();
            System.out.println("作者名称 " + authorName);

            A.DataDTO.VisionProfilePhotoListDTO.FeedsDTO.PhotoDTO photo = feedsDTO.getPhoto();

            // 视频唯一id(待确认是否是全局唯一)
            String videoId = photo.getId();
            System.out.println("视频唯一id " + videoId);
            // 动态缩略图
            String animatedCoverUrl = photo.getAnimatedCoverUrl();
            System.out.println("动态缩略图 " + animatedCoverUrl);
            // 封面图
            String coverUrl = photo.getCoverUrl();
            System.out.println("封面图 " + coverUrl);
            // 标题
            String caption = photo.getCaption();
            System.out.println("标题 " + caption);
            // 视频地址高清
            String photoUrl = photo.getPhotoUrl();
            System.out.println("视频地址高清 " + photoUrl);

            // 视频地址低清
            String photoH265Url = photo.getPhotoH265Url();
            System.out.println("视频地址低清 " + photoH265Url);

            Video video = new Video();
            video.setVideoId(videoId);
            video.setAnimatedOverUrl(animatedCoverUrl);
            video.setCoverUrl(coverUrl);
            video.setVideoCaption(caption);
            video.setPhotoUrl(photoUrl);
            video.setPhotoH265Url(photoH265Url);
            video.setAuthorId(authorId);
            video.setHeaderUrl(headerUrl);
            video.setAuthorName(authorName);
            video.setCreateTime(new Date());

            videoList.add(video);
        }

        System.out.println(videoList);


//https://v1.kwaicdn.com/upic/2023/02/04/00/BMjAyMzAyMDQwMDE1MzRfMjUyMjkxNzAzMl85NTM4MTE4NTQ1NV8xXzM=_hd15_B0414255f80661494013ce37752ba9b51.mp4?pkey=AAVy7LCeD5E0SA3QDN7F5q_AmownVpTHMd-D_CumcXYNxFKa_qN5BFyK2KrgazE57_nAqVWX4Vm03Fsii5Ky-jnMi5rLjxBzxqKfG6eP5o-zZpmlXJXCcTE-3yNK07jTCmY&tag=1-1676102168-unknown-0-lzpsnpdbau-b1360c978538786d&clientCacheKey=3xd9hk46abwv2ty_hd15.mp4&di=78ed6894&bp=14734&tt=hd15&ss=vp
//https://v2.kwaicdn.com/upic/2023/02/04/00/BMjAyMzAyMDQwMDE1MzRfMjUyMjkxNzAzMl85NTM4MTE4NTQ1NV8xXzM=_b_Bc9f8903f83a4f646232902377191c89f.mp4?pkey=AAWjCN7HNpllcE4I0K33JDAGz5f5EC3Nxx-XdPblhHEqLUscc0mAwCd694QEMf--deHsD_9CaF8BkId6Y4rayJTLtXkHpu7aRAw24uKJoumtEvN11B4fVyd-VE0NSCnWGg4&tag=1-1676102168-unknown-0-z6xezzakdd-dc434de04e2cfd7c&clientCacheKey=3xd9hk46abwv2ty_b.mp4&di=78ed6894&bp=14734&tt=b&ss=vp

    }



}
